const sqlhelper = require("./sqlhelper");
const connection = sqlhelper.sqlConnection;

module.exports = {
  getAllPosts(callback) {
    let sql = `SELECT posts.id,title,posts.\`status\`,created,nickname,\`name\`
              FROM  posts
              JOIN users ON posts.user_id=users.id
              JOIN categories ON posts.category_id=categories.id`;
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  },
  getPostByPage(pageIndex, pageSize, callback) {
    // limit （第几页-1）页容量，页容量
    let sql = `SELECT posts.id,title,posts.\`status\`,created,nickname,\`name\`
              FROM  posts
              JOIN users ON posts.user_id=users.id
              JOIN categories ON posts.category_id=categories.id
              LIMIT ${(pageIndex - 1) * pageSize},${pageSize}`;
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  }
};
